#ifndef __TM1638_H__
#define __TM1638_H__
//#include "stm32f10x.h"
//#include "sys.h"
//#include "delay.h"
#include "my_include.h"

extern u8 DispTab[4];
extern u8 Update_disp_flg;

#define TM1638_STB                 PCout(7)
#define TM1638_CLK                 PCout(8)
#define TM1638_DIO_IN              {GPIOC->CRH&=0XFFFFFF0F;GPIOC->CRH|=(u32)8<<4;}
#define TM1638_DIO_OUT             {GPIOC->CRH&=0XFFFFFF0F;GPIOC->CRH|=(u32)3<<4;}             
#define TM1638_DIO_DataOUT         PCout(9)
#define TM1638_DIO_DataIN          PCin(9)

//地址命令设置
//GRID1 硬件引脚
#define TM1638_GRID1  0xc0
#define TM1638_GRID2  0xc1
//GRID2 硬件引脚
#define TM1638_GRID3  0xc2
#define TM1638_GRID4  0xc3
//GRID3 硬件引脚
#define TM1638_GRID5  0xc4
#define TM1638_GRID6  0xc5
//GRID4 硬件引脚
#define TM1638_GRID7  0xc6
#define TM1638_GRID8  0xc7
//GRID5 硬件引脚
#define TM1638_GRID9  0xc8
#define TM1638_GRID10 0xc9
//GRID6 硬件引脚
#define TM1638_GRID11 0xca
#define TM1638_GRID12 0xcb
//GRID7 硬件引脚
#define TM1638_GRID13 0xcc
#define TM1638_GRID14 0xcd
//GRID8 硬件引脚
#define TM1638_GRID15 0xce
#define TM1638_GRID16 0xcf


//数据命令设置
/*
#define 0x40   //写数据到显示寄存器
#define 0x42   //读键扫数据
#define 0x40   //自动地址增加
#define 0x44   //固定地址
#define 0x40   //普通模式
#define 0x48   //测试模式
*/

//显示
/*
#define 0x80   //显示关
#define 0x88   //显示开
*/

typedef union _tm1638_byte
{
  uint8_t  Byte;
  struct
  {
    uint8_t  bit0:1;
    uint8_t  bit1:1;
    uint8_t  bit2:1;
    uint8_t  bit3:1;
    uint8_t  bit4:1;
    uint8_t  bit5:1;
    uint8_t  bit6:1;
    uint8_t  bit7:1;
  }bit;
}tm1638_byte_t;
//按键值
#define Key_Set             (0x0004)   //设置按键
#define Key_Return          (0x0040)   //放回按键
#define Key_ADD             (0x0002)   //加号按键 （+）
#define Key_SUD             (0x0020)   //减号按键 （-）
#define Key_BLOOP           (0x0001)   //消音按键
#define Key_TEST            (0x0010)   //测试按键


/*********按键数据*****************/
typedef union _TM1638_KEY_32_T
{
    uint32_t  HEX_32;
    uint16_t  HEX_16[2];
    uint8_t   HEX_8[4];
}KEY_TypeDef;
extern KEY_TypeDef KeyValue;              //保存按键值

///*********led指示灯数据*****************/
//#if PT100_TEMP_EN
//typedef union _TM1638_LED_32_T
//{
//    uint16_t   HEX_16;
//    struct
//    {
//        uint8_t Sys_Run:1;             // 运行灯 蓝色
//        uint8_t Sys_Power:1;             // 运行灯 红色
//      
//        uint8_t alarm_blue_1:1;         //ADC  PT100_1  告警状态灯 蓝色
//        uint8_t alarm_red_1:1;          //ADC  PT100_1  告警状态灯 红色
//      
//        uint8_t alarm_blue_2:1;         //ADC  PT100_2  告警状态灯 蓝色
//        uint8_t alarm_red_2:1;          //ADC  PT100_2  告警状态灯 红色
//      
//        uint8_t alarm_blue_3:1;         //ADC  PT100_3  告警状态灯 蓝色
//        uint8_t alarm_red_3:1;          //ADC  PT100_3  告警状态灯 红色
//              
//        uint8_t alarm_blue_4:1;         //ADC  PT100_4  告警状态灯 蓝色
//        uint8_t alarm_red_4:1;          //ADC  PT100_4  告警状态灯 红色
//      
//        uint8_t res: 6;                //备用
//    }bit;
//    
//}LED_TypeDef;

//#endif

#if  HLW8112_EN

typedef union _TM1638_LED_1
{
    uint16_t    HEX_16;
    struct
    {
        uint8_t Power_RedLed:1;          // 电源灯 红色
        uint8_t Power_blueLed:1;         // 电源灯 蓝色
      
        uint8_t Run_RedLed:1;            //运行状态 红色
        uint8_t Run_blueLed:1;           //运行状态 蓝色
     
        uint8_t Net_RedLed:1;            //网络状态 红色
        uint8_t Net_blueLed:1;           //网络状态 蓝色
      
        uint8_t WLAN_RedLed:1;           //WLAN无线网络状态 红色
        uint8_t WLAN_blueLed:1;          //WLAN无线网络状态 蓝色
      
        uint8_t Blue_RedLed:1;           //蓝牙状态 红色
        uint8_t Blue_blueLed:1;          //蓝牙状态 蓝色
              
        uint8_t res: 6;                 //备用
    }bit;
    
}union_LED_1_TypeDef;

typedef union _TM1638_LED_2
{
    uint16_t    HEX_16;
    struct
    {
        
        uint8_t alarm_red_s:1;          // 告警状态灯 红色
        uint8_t alarm_blue_s:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_1:1;          // 告警状态灯 红色
        uint8_t alarm_blue_1:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_2:1;          // 告警状态灯 红色
        uint8_t alarm_blue_2:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_3:1;          // 告警状态灯 红色
        uint8_t alarm_blue_3:1;         // 告警状态灯 蓝色      
        
        uint8_t alarm_red_4:1;          // 告警状态灯 红色  
        uint8_t alarm_blue_4:1;         // 告警状态灯 蓝色
        uint8_t res: 8;                 //备用
    }bit;
    
}union_LED_2_TypeDef;

typedef union _TM1638_LED_3
{
    uint16_t    HEX_16;
    struct
    {
       
        uint8_t alarm_red_5:1;          // 告警状态灯 红色
        uint8_t alarm_blue_5:1;         // 告警状态灯 蓝色
       
        uint8_t alarm_red_6:1;          // 告警状态灯 红色
        uint8_t alarm_blue_6:1;         // 告警状态灯 蓝色
       
        uint8_t alarm_red_7:1;          // 告警状态灯 红色
        uint8_t alarm_blue_7:1;         // 告警状态灯 蓝色       
       
        uint8_t alarm_red_8:1;          // 告警状态灯 红色
        uint8_t alarm_blue_8:1;         // 告警状态灯 蓝色
        uint8_t res: 8;                //  备用
      
    }bit;
    
}union_LED_3_TypeDef;
typedef union _TM1638_LED_4
{
    uint16_t    HEX_16;
    struct
    {
        
        uint8_t alarm_red_9:1;          // 告警状态灯 红色
        uint8_t alarm_blue_9:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_10:1;          // 告警状态灯 红色
        uint8_t alarm_blue_10:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_11:1;          // 告警状态灯 红色
        uint8_t alarm_blue_11:1;         // 告警状态灯 蓝色
        
        uint8_t alarm_red_12:1;          // 告警状态灯 红色
        uint8_t alarm_blue_12:1;         // 告警状态灯 蓝色
      
        uint8_t res: 8;                  //备用
    }bit;
    
}union_LED_4_TypeDef;

typedef struct _TM1638_LED_T
{
  union_LED_1_TypeDef uled1;
  union_LED_2_TypeDef uled2;
  union_LED_3_TypeDef uled3;
  union_LED_4_TypeDef uled4;

}LED_TypeDef;

#endif

extern LED_TypeDef LedValue;              //保存led灯

///*****************物联ADC板子修改led灯*******************************/
//#define Sys_RunBlueLed(s)   Digital_led(1,1,s)   //系统运行灯

//#define ADC_Sensor1LED(s)   Digital_led(1,2,s)   //ADC  PT100_1  告警状态灯
//#define ADC_Sensor2LED(s)   Digital_led(1,3,s)   //ADC  PT100_2  告警状态灯
//#define ADC_Sensor2LED(s)   Digital_led(1,4,s)   //ADC  PT100_3  告警状态灯
//#define ADC_Sensor2LED(s)   Digital_led(1,5,s)   //ADC  PT100_4  告警状态灯

///*****************新版动环修改led灯*******************************/
//#define SysBlueLed(s)        Digital_led(8,1,s)   //系统运行灯
//#define NetRedLed(s)         Digital_led(8,2,s)   //网络状态灯
//#define PwrONULed(s)         Digital_led(8,3,s)   //光猫电源灯
//#define PwrPBXLed(s)         Digital_led(8,4,s)   //交换机电源灯
//#define PmokeLed(s)          Digital_led(8,5,s)   //烟雾状态灯

//#define WaterLed(s)          Digital_led(8,6,s)   //水浸状态灯
//#define InfraredLed(s)       Digital_led(8,7,s)   //红外状态灯
//#define Door_Sensor1LED(s)   Digital_led(8,8,s)   //门磁1状态灯
//#define Door_Sensor2LED(s)   Digital_led(9,1,s)   //门磁2状态灯
//#define Door_ControlLED(s)   Digital_led(9,2,s)   //门禁状态灯


///******************老物联双色LED灯**************************/
//#define SysBlueLed(s)        Digital_led(8,1,s)
//#define NetRedLed(s)         Digital_led(8,2,s)
//#define PwrBlueLed(s)        Digital_led(8,3,s)
//#define PwrRedLed(s)         Digital_led(8,4,s)
//#define RS485BlueLed(s)      Digital_led(8,5,s)
//#define GSMRedLed(s)         Digital_led(8,6,s)
//#define NetLinkBlueLed(s)    Digital_led(8,7,s)
//#define reserved1(s)         Digital_led(8,8,s)

//#define ExchangerBlueLed(s)  Digital_led(9,1,s)
//#define reserved2(s)         Digital_led(9,2,s)
//#define Camera1BlueLed(s)    Digital_led(10,1,s)
//#define reserved3(s)         Digital_led(10,2,s)

//#define Camera2BlueLed(s)    Digital_led(10,3,s)
//#define reserved4(s)         Digital_led(10,4,s)
//#define Camera3BlueLed(s)    Digital_led(10,5,s)
//#define reserved5(s)         Digital_led(10,6,s)
//#define Camera4BlueLed(s)    Digital_led(10,7,s)
//#define reserved6(s)         Digital_led(10,8,s)
//#define reserved7(s)         Digital_led(11,1,s)
//#define reserved8(s)         Digital_led(11,2,s)



//#define LED1_BLUE(s)        Digital_led(8,1,s)//LED1蓝灯
//#define LED1_RED(s)         Digital_led(8,2,s)//LED1红灯


//#define LED2_BLUE(s)        Digital_led(8,3,s)//LED2蓝灯
//#define LED2_RED(s)         Digital_led(8,4,s)//LED2红灯


//#define LED3_BLUE(s)        Digital_led(8,5,s)//LED3蓝灯
//#define LED3_RED(s)         Digital_led(8,6,s)//LED3红灯


//#define LED4_BLUE(s)        Digital_led(8,7,s)//LED4蓝灯
//#define LED4_RED(s)         Digital_led(8,8,s)//LED4红灯

//#define LED5_BLUE(s)        Digital_led(9,1,s)//LED5蓝灯
//#define LED5_RED(s)         Digital_led(9,2,s)//LED5红灯


//#define LED6_BLUE(s)        Digital_led(10,1,s)//LED6蓝灯
//#define LED6_RED(s)         Digital_led(10,2,s)//LED6红灯


//#define LED7_BLUE(s)        Digital_led(10,3,s)//LED7蓝灯
//#define LED7_RED(s)         Digital_led(10,4,s)//LED7红灯


//#define LED8_BLUE(s)        Digital_led(10,5,s)//LED8蓝灯
//#define LED8_RED(s)         Digital_led(10,6,s)//LED8红灯


//#define LED9_BLUE(s)        Digital_led(10,7,s)//LED9蓝灯
//#define LED9_RED(s)         Digital_led(10,8,s)//LED9红灯


//#define LED10_BLUE(s)       Digital_led(11,1,s)//LED10蓝灯
//#define LED10_RED(s)        Digital_led(11,2,s)//LED10红灯

void led_handle(void);
void key_handle(void);
void Read_key(u8 *KeyValue);
void TM1638_Init(void);
void dispclear(u8 n,u8 m);
void Flicker(uint8_t n);
void display_led(u8 dig,u8 dat);
void display(u8 dig,u8 dat);
void DigitalTube_led(u8 dig,u8 Data,u8 onoff);
void UpdateDisp(void);
void Write_DATA(u8 add,u8 DATA);

void Digital_led(u8 dig,u8 seg,u8 onoff);
void DigitalTube_led(u8 dig,u8 seg,u8 onoff);

void TM1638_Digital_LED_SET(uint8_t GRID, uint16_t led_value);
void TM1638_keytime_handle(void);  
void led_Temp_alarm_show(uint8_t num,uint8_t state);
#endif

